package com.lecture.springlecture.controller;

import com.lecture.springlecture.entity.Icon;
import com.lecture.springlecture.service.IconService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.Base64;
import java.util.List;

@RestController
@RequestMapping("icon")
public class IconController {

    @Autowired
    private IconService iconService;

    @PostMapping("uploadIcon")
    public void uploadIcon(String name, MultipartFile file) {
        try {
            Icon icon = new Icon();
            icon.setName(name);

            icon.setIcon(Base64.getEncoder().encodeToString(file.getBytes()));
            iconService.save(icon);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @GetMapping("getIconList")
    public List<Icon> getIconList() {
        return iconService.list();
    }
}
